package com.easyOrder.core.json;

import com.easyOrder.core.exception.ApplicationException;
import com.easyOrder.core.exception.ApplicationErrorCode;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;

public class JsonExceptionWrapper {

    private String code;

    private String msg;

    public JsonExceptionWrapper(Throwable ex) {
        if (ex instanceof ApplicationException) {
            ApplicationException gex = (ApplicationException) ex;
            this.code = gex.getCode();
            this.msg = gex.getReasoning();
        } else if (ex instanceof IllegalArgumentException
                || ex instanceof MissingServletRequestParameterException
                || ex instanceof HttpMessageNotReadableException
                || ex instanceof HttpRequestMethodNotSupportedException
                || ex instanceof HttpMediaTypeNotSupportedException
                || ex instanceof MethodArgumentTypeMismatchException) {
            this.code = ApplicationErrorCode.ArgumentsIncorrect.getCode();
            this.msg = ex.getMessage();
        }
        else if (ex instanceof IOException){
            this.code = ApplicationErrorCode.IOException.getCode();
            this.msg = ex.getMessage();
        }
        else {
            this.code = ApplicationErrorCode.UnKnowException.getCode();
            this.msg = ApplicationErrorCode.UnKnowException.getReasoning();
        }
    }

    public static String getStackTrace(Throwable throwable) {
        if (throwable == null) {
            return "";
        }
        String stackTrace = Arrays.toString(throwable.getStackTrace());
        try {
            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            throwable.printStackTrace(printWriter);
            printWriter.flush();
            writer.flush();
            stackTrace = writer.toString();
            printWriter.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stackTrace;
    }

    public JsonExceptionWrapper() {
        super();
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "ErrorJsonWrapper [code=" + code + ", msg=" + msg + "]";
    }
}
